| Conditions | 1 |
| Paths | 2 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 15 | function upgrades(state, visibility, upgrade, data) { |
||
| 16 | let ct = this; |
||
| 17 | ct.state = state; |
||
| 18 | ct.data = data; |
||
| 19 | let sortFunc = [ |
||
| 20 | (a,b) => data.upgrades[a].name < data.upgrades[b].name ? -1 : 1, |
||
| 21 | (a,b) => data.upgrades[a].price - data.upgrades[b].price |
||
| 22 | ] |
||
| 23 | |||
| 24 | // tries to buy all the upgrades it can, starting from the cheapest |
||
| 25 | ct.buyAll = function (slot) { |
||
| 26 | let currency = data.elements[slot.element].main; |
||
| 27 | let cheapest; |
||
| 28 | let cheapestPrice; |
||
| 29 | do{ |
||
| 30 | cheapest = null; |
||
| 31 | cheapestPrice = Infinity; |
||
|
|
|||
| 32 | for(let up of ct.visibleUpgrades(slot, data.upgrades)){ |
||
| 33 | let price = data.upgrades[up].price; |
||
| 34 | if(!slot.upgrades[up] && |
||
| 35 | price <= state.player.resources[currency].number){ |
||
| 36 | if(price < cheapestPrice){ |
||
| 37 | cheapest = up; |
||
| 38 | cheapestPrice = price; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | if(cheapest){ |
||
| 43 | upgrade.buyUpgrade(state.player, |
||
| 44 | slot.upgrades, |
||
| 45 | data.upgrades[cheapest], |
||
| 46 | cheapest, |
||
| 47 | cheapestPrice, |
||
| 48 | currency); |
||
| 49 | } |
||
| 50 | }while(cheapest); |
||
| 51 | }; |
||
| 52 | |||
| 53 | ct.buyUpgrade = function (name, slot) { |
||
| 54 | let price = data.upgrades[name].price; |
||
| 55 | let currency = data.elements[slot.element].main; |
||
| 56 | upgrade.buyUpgrade(state.player, |
||
| 57 | slot.upgrades, |
||
| 58 | data.upgrades[name], |
||
| 59 | name, |
||
| 60 | price, |
||
| 61 | currency); |
||
| 62 | }; |
||
| 63 | |||
| 64 | ct.visibleUpgrades = function(slot) { |
||
| 65 | return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[state.sort]); |
||
| 66 | }; |
||
| 67 | |||
| 68 | function isBasicUpgradeVisible(name, slot) { |
||
| 69 | let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name]); |
||
| 70 | return isVisible && (!state.hideBought || !slot.upgrades[name]); |
||
| 71 | } |
||
| 72 | } |
||
| 73 |